#!/usr/bin/node
class Queue {
    constructor() {
      this.data = [];
    }
  
    write(data) {
      this.data.push(data);
      console.log(`Queue: ${this.data}`);
    }
  
    read() {
      if (this.data.length === 0) {
        return null;
      }
      const data = this.data.shift();
      console.log(`Queue: ${this.data}`);
      return data;
    }
  }
  
  class Producer {
    constructor(queue) {
      this.queue = queue;
    }
  
    create(data) {
      console.log(`+ prd create: ${data}`);
      this.queue.write(data);
      // 模拟生产一个数据后，立即尝试消费一个数据
      consumer.destroy();
    }
  }
  
  class Consumer {
    constructor(queue) {
      this.queue = queue;
    }
  
    destroy() {
      const data = this.queue.read();
      if (data !== null) {
        console.log(`- csm destroy: ${data}`);
        // 模拟消费一个数据后，立即生产下一个数据
        producer.create(data + 1);
      } else {
        console.log('Queue is empty');
      }
    }
  }
  
  const queue = new Queue();
  const producer = new Producer(queue);
  const consumer = new Consumer(queue);
  
  function main() {
    producer.create(0); // 启动生产者和消费者循环
  }
  
  main();